#!/bin/sh
#
#  xfce4
#
#  Copyright (C) 1999, 2003 Olivier Fourdan (fourdan@xfce.org)
#  Copyright (C) 2011       Guido Berhoerster (guido+xfce.org@berhoerster.name)
#  Copyright (C) 2015, 2018 Simon Steinbeiß (simon@xfce.org)
#  Copyright (C) 2011, 2022 Jarno Suni (8@iki.fi)
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

# Wait a bit so that user have time to raise finger from a key if
# the script is run in terminal.
[ -t 0 ] && sleep 1

# First test for the command set in the session's xfconf channel
# Do not try anything else it that fails: if changed, this behavior should be kept
# in sync with XfceScreensaver (libxfce4ui)
LOCK_CMD=$(xfconf-query -c xfce4-session -p /general/LockCommand)

if [ -n "$LOCK_CMD" ]; then
    /bin/sh -c "$LOCK_CMD" && exit
    exit_code=$?
    >&2 printf "'%s' exited with status %d\n" "$LOCK_CMD" $exit_code
    exit $exit_code
fi

# Try our screensaver first
xfce4-screensaver-command --lock </dev/null 2>/dev/null && exit

# This list should be kept in sync with XfceScreensaver (libxfce4ui)
xdg-screensaver lock </dev/null 2>/dev/null && exit
for lock_cmd in \
    xscreensaver-command \
    light-locker-command \
    gnome-screensaver-command \
    mate-screensaver-command
do
    $lock_cmd --lock </dev/null 2>/dev/null && exit
done

# else access locking failed
exit 1
